<?php
/**
 * 后台商品管理控制器 
 */
class GoodsController extends Controller {
	public $layout='application.modules.admin.views.layouts.mycolumn';
    /*
     * 商品展示
     */
	 
    function actionShow(){

    	$goods_model = Goods::model();

 		
 		/* 
 		if(isset($_GET['order_column'])){
 			if(isset($_GET['order_desc'])){
 				$order = $_GET['order_column']." ".$_GET['order_desc'];
 				$order_desc = '';
 			}else{
 				$order = $_GET['order_column'];
 				$order_desc = 'desc';
 			}
 		}else{
 			$order = '';
 			$order_desc = 'desc';
 		} 
 		*/
 		$cnt = $goods_model->count();
 		$per = 10;
 		$page = new Pagination($cnt,$per);
 		
    	$condition =array(
    			'select'=>'*',
    			'order'=>'goods_id',
    			'condition'=>'',
    			'offset'=>$page->offset,
    			'limit'=>$page->limit,
    	);
    	
    	$goods_info = $goods_model -> findAll($condition);
    	/* 
    	//通过model模型对象调用相关方法帮我们查询数据
    	$goods_info = $goods_model -> find();  //每次只可以查询一条商品信息
    	var_dump($goods_info);
    	echo $goods_info->goods_name,"<br />";
    	echo $goods_info->goods_price,"<br />";
    	*/
    	
    	/* 
    	//获得全部商品信息findAll()
    	$goods_info = $goods_model -> findAll();
    	//获得全部商品名字的信息
    	foreach($goods_info as $_v){
    	//$_v就是遍历出来的具体对象
    	    echo $_v->goods_name,"----",$_v->goods_price,"<br />";
    	}
    	*/
    	
    	/* 
    	//通过具体sql语句获得商品信息findAllBySql()
    	$sql = "select goods_name,goods_price,goods_create_time from {{goods}} limit 10";
    	$goods_info = $goods_model ->findAllBySql($sql);
    	*/
    	
    	
    	//把获得数据信息传递到视图模板里边
    	//renderPartial('视图名字',传递的变量信息);
    	//renderPartial('show',array('名字'=>值,'名字'=>值));
    	//名字：是视图使用的变量名字
    	//值：当前被传递变量的值
    	
    	$page_list = $page->fpage(array(3,4,5,6,7));
        //$this ->renderPartial('show',array("goods_info"=>$goods_info,"order_desc"=>$order_desc,'page_list'=>$page_list));
    	$this ->renderPartial('show',array("goods_info"=>$goods_info,'page_list'=>$page_list));
    }
    
    /*
     * 添加商品
     */
    function actionAdd(){
    	$goods_model = new Goods();

    	//var_dump($_POST);
    	if(isset($_POST['Goods'])){
    		foreach ($_POST['Goods'] as $key=>$value)
    		{
    			$goods_model->$key = $value;
    		}
    		$goods_model -> goods_create_time = time();
    		//var_dump($goods_model);
    		if($goods_model -> save())
    		{
    			$this -> redirect('./index.php?r=admin/goods/show');
    			//Yii::success("提示信息内容","跳转链接","停留时间，单位秒");
    			//Yii::success("商品信息发布成功","index.php?r=admin/goods/show","3");
    			//exit;
    		}else{
    			//Yii::error("商品信息发布失败","index.php?r=admin/goods/add","3");
    			//exit;
    		}
    			
    	}
    	
        $this ->renderPartial('add',array('goods_model'=> $goods_model));
    }
    
    /*
     * 修改商品
     */
    function actionUpdate($id){
        //我们具体修改哪个商品，需要将其信息查询出来
        //我们需要知道哪个商品被修改，把商品的id信息通过get方式传递过来
        //接收被修改商品id信息
       
        //根据$id查询被修改商品信息
        $goods_model = Goods::model();  //除了添加(new Goods)数据我们都使用Goods::model()来实例化模型对象
        $goods_info = $goods_model -> findByPk($id);
        
        //修改逻辑与添加逻辑基本一致，创建表单、收集数据、赋予模型、调用save方法
        //修改的时候，数据也是赋予数据模型对象里边了
        //$goods_info数据模型里边有一些旧的信息，新的信息覆盖旧的信息
        if(isset($_POST['Goods'])){
            foreach($_POST['Goods'] as $_k => $_v){
                $goods_info -> $_k = $_v;
            }
            
        	if($goods_info -> save())
    		{
    			$this -> redirect('./index.php?r=admin/goods/show');
    			//Yii::success("提示信息内容","跳转链接","停留时间，单位秒");
    			//Yii::success("商品信息发布成功","index.php?r=admin/goods/show","3");
    			//exit;
    		}else{
    			//Yii::error("商品信息发布失败","index.php?r=admin/goods/add","3");
    			//exit;
    		}
        }
        //创建数据模型model对象
        //new  Goods()  ;   调用save方法的时候给我们执行insert语句
        //Goods::model();    调用save方法的时候执行update语句
        
        //$goods_info是我们查询出来的被修改商品的信息，同时也是数据模型对象
        //把$goods_info传递到视图模板里边。
        
        $this ->renderPartial('update', array('goods_model'=>$goods_info));
    }
    
    /*
     * 删除商品信息
     * 删除商品信息与修改类似，通过get方式传递被删除商品信息id
     */
    function actionDel($id){
    	//根据$id将被删除商品的数据模型对象获得到，通过该对象调用 delete 方法即可删除数据
    	$goods_model = Goods::model(); //获得数据模型对象
    	$goods_info = $goods_model -> findByPk($id);  //获得被删除商品的模型对象
    	//是谁($goods_info还是$goods_model)调用delete
    	if($goods_info->delete())
    		$this -> redirect('./index.php?r=admin/goods/show');
    }
    
    function actionInsert(){
    	//1 创建模型对象出来
    	$goods_model = new Goods();	 //需要添加数据，创建对象方式有别与查询
    	
    	//2. 为对象丰富属性 goods_name,goods_price,goods_weight，等等
    	$goods_model -> goods_name = "Apple 5s";
    	$goods_model -> goods_price = 5199;
        $goods_model -> goods_weight = 102;
       	$goods_model -> goods_create_time = time(); 
         //3. 调用save()方法实现数据添加
        if($goods_model -> save())
            echo "success";
        else 
            echo "fail";
    	
    }
    
}